package com.lagou.zuoye;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author 尘
 * @data 2022/5/13 10:36
 */
public class DBUtils {

    public static String DRIVERNAME;
    public static String URL;
    public static String USER;
    public static String PASSWORD;

    static {
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read("J:\\jdbc_work\\xml_task03\\src\\com\\lagou\\zuoye\\employee.xml");


            Node nodeDrive = document.selectSingleNode("/dbu/employe[@name='driverClass']");
            DRIVERNAME = nodeDrive.getText();

            Node nodeUrl = document.selectSingleNode("/dbu/employe[@name='jdbcUrl']");
            URL = nodeUrl.getText();

            Node nodeUser = document.selectSingleNode("/dbu/employe[@name='user']");
            USER = nodeUser.getText();

            Node nodePassWord = document.selectSingleNode("/dbu/employe[@name='password']");
            PASSWORD = nodePassWord.getText();

            Class.forName(DRIVERNAME);

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    public static Connection getConnection(){
        try {
            Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);
            return connection;

        } catch (SQLException e){
            e.printStackTrace();
            return null;

        }
    }

}
